package cn.edu.zjgsu.ito.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.zjgsu.ito.dao.CourierOrderMapper;
import cn.edu.zjgsu.ito.dao.DemandOrderMapper;
import cn.edu.zjgsu.ito.dao.DemandOrderViewMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderStoreMapper;
import cn.edu.zjgsu.ito.dao.LatitudeLongitudeMapper;
import cn.edu.zjgsu.ito.dao.OrderNoCourierMapper;
import cn.edu.zjgsu.ito.dao.OrderTableMapper;
import cn.edu.zjgsu.ito.dao.PurchaseOrderMapper;
import cn.edu.zjgsu.ito.dao.UserAddressMapper;
import cn.edu.zjgsu.ito.dao.UserCommentsMapper;
import cn.edu.zjgsu.ito.dao.UserInfomationMapper;
import cn.edu.zjgsu.ito.dao.UserRefundMapper;
import cn.edu.zjgsu.ito.dao.UserScoreMapper;
import cn.edu.zjgsu.ito.dao.UserVouchersMapper;
import cn.edu.zjgsu.ito.dao.VouchersMapper;
import cn.edu.zjgsu.ito.model.CourierOrder;
import cn.edu.zjgsu.ito.model.CourierOrderExample;
import cn.edu.zjgsu.ito.model.DemandOrder;
import cn.edu.zjgsu.ito.model.DemandOrderExample;
import cn.edu.zjgsu.ito.model.DemandOrderView;
import cn.edu.zjgsu.ito.model.DemandOrderViewExample;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrder;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderExample;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderStore;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderStoreExample;
import cn.edu.zjgsu.ito.model.LatitudeLongitude;
import cn.edu.zjgsu.ito.model.LatitudeLongitudeExample;
import cn.edu.zjgsu.ito.model.OrderNoCourier;
import cn.edu.zjgsu.ito.model.OrderNoCourierExample;
import cn.edu.zjgsu.ito.model.OrderTable;
import cn.edu.zjgsu.ito.model.OrderTableExample;
import cn.edu.zjgsu.ito.model.PurchaseOrder;
import cn.edu.zjgsu.ito.model.UserAddress;
import cn.edu.zjgsu.ito.model.UserComments;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserRefund;
import cn.edu.zjgsu.ito.model.UserRefundExample;
import cn.edu.zjgsu.ito.model.UserScore;
import cn.edu.zjgsu.ito.model.UserVouchers;
import cn.edu.zjgsu.ito.model.Vouchers;
import cn.edu.zjgsu.ito.service.UserPurchaseServiceI;
import cn.edu.zjgsu.ito.util.GetTime;
import cn.edu.zjgsu.ito.util.OrderStateConstant;
import cn.edu.zjgsu.ito.vo.DemandIdVo;
import cn.edu.zjgsu.ito.vo.DeterminePriceSaveVo;
import cn.edu.zjgsu.ito.vo.FindCourierSaveVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.OrderNoCourierShowVo;
import cn.edu.zjgsu.ito.vo.OrderStateShowVo;
import cn.edu.zjgsu.ito.vo.PayCourierPriceSaveVo;
import cn.edu.zjgsu.ito.vo.PayCourierPriceShowVo;
import cn.edu.zjgsu.ito.vo.PurchaseCommentsSaveVo;
import cn.edu.zjgsu.ito.vo.PurchaseHomeShowVo;
import cn.edu.zjgsu.ito.vo.RefundReasonSaveVo;
import cn.edu.zjgsu.ito.vo.UserAddressVo;
import cn.edu.zjgsu.ito.vo.DemandMessageShowVo;
import cn.edu.zjgsu.ito.vo.UserHomeMessageShowVo;

@Service("userPurchaseService")
public class UserPurchaseServiceImpl implements UserPurchaseServiceI {

	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;

	@Autowired
	private DemandOrderMapper demandOrderMapper;

	@Autowired
	private OrderTableMapper orderTableMapper;

	@Autowired
	private UserAddressMapper userAddressMapper;

	@Autowired
	private OrderNoCourierMapper orderNoCourierMapper;
	
	@Autowired
	private DemandOrderViewMapper demandOrderViewMapper;
	
	@Autowired
	private UserInfomationMapper userInfomationMapper;
	
	@Autowired
	private UserScoreMapper userScoreMapper;
	
	@Autowired
	private ImageUrlDemandOrderMapper imageUrlDemandOrderMapper;
	
	@Autowired
	private ImageUrlDemandOrderStoreMapper imageUrlDemandOrderStoreMapper;
	
	@Autowired
	private UserRefundMapper userRefundMapper;
	
	@Autowired
	private LatitudeLongitudeMapper latitudeLongitudeMapper;
	
	@Autowired
	private CourierOrderMapper courierOrderMapper;
	
	@Autowired
	private UserVouchersMapper userVouchersMapper;
	
	@Autowired
	private VouchersMapper vouchersMapper;
	
	@Autowired
	private UserCommentsMapper userCommentsMapper;

	
	public List<PurchaseHomeShowVo> homePage(){
		
		List<PurchaseHomeShowVo> purchaseHomeShowVos = new ArrayList<PurchaseHomeShowVo>();
		
		DemandOrderViewExample example = new DemandOrderViewExample();
		example.or().andDemandStateEqualTo(OrderStateConstant.DEMAND_NO_ACCEPT);
//		example.setOrderByClause("demand_createtime DESC");
		List<DemandOrderView> demandOrders = demandOrderViewMapper.selectByExample(example);
		System.out.println("size：" + demandOrders.size());
		for(int i = 0;i < demandOrders.size();i++){
			PurchaseHomeShowVo purchaseHomeShowVo = new PurchaseHomeShowVo();
			
			BeanUtils.copyProperties(demandOrders.get(i), purchaseHomeShowVo);
			purchaseHomeShowVo.setDemandCreatetime(GetTime.dateToStr(demandOrders.get(i).getDemandCreatetime()));
			
			purchaseHomeShowVos.add(purchaseHomeShowVo);
		}
		return purchaseHomeShowVos;
	}
	
	@Override
	public List<PurchaseHomeShowVo> orderByHomePage(int temp) {

		List<PurchaseHomeShowVo> purchaseHomeShowVos = new ArrayList<PurchaseHomeShowVo>();
		List<DemandOrderView> demandOrders = null;
		DemandOrderViewExample example = new DemandOrderViewExample();
		example.or().andDemandStateEqualTo(OrderStateConstant.DEMAND_NO_ACCEPT);
		
		if(temp == 0){//创建时间
			example.setOrderByClause("demand_createtime DESC");
		}else if(temp == 1){//小费
//			example.setOrderByClause("demand_createtime DESC");
		}
		demandOrders = demandOrderViewMapper.selectByExample(example);
		for(int i = 0;i < demandOrders.size();i++){
			PurchaseHomeShowVo purchaseHomeShowVo = new PurchaseHomeShowVo();
			
			BeanUtils.copyProperties(demandOrders.get(i), purchaseHomeShowVo);
			purchaseHomeShowVo.setDemandCreatetime(GetTime.dateToStr(demandOrders.get(i).getDemandCreatetime()));
			
			purchaseHomeShowVos.add(purchaseHomeShowVo);
		}
		
		return purchaseHomeShowVos;
		
	}
	
	
	@Override
	public List<PurchaseHomeShowVo> seletByHomePage(String temp) {
		List<PurchaseHomeShowVo> purchaseHomeShowVos = new ArrayList<PurchaseHomeShowVo>();

		DemandOrderViewExample example = new DemandOrderViewExample();
		example.or().andDemandStateEqualTo(OrderStateConstant.DEMAND_NO_ACCEPT).andTradeNameLike("%" + temp + "%");//模糊搜索
		example.or().andDemandStateEqualTo(OrderStateConstant.DEMAND_NO_ACCEPT).andPurchasePlaceLike("%" + temp + "%");
		List<DemandOrderView> demandOrders = demandOrderViewMapper.selectByExample(example);
		System.out.println("size：" + demandOrders.size());
		for(int i = 0;i < demandOrders.size();i++){
			PurchaseHomeShowVo purchaseHomeShowVo = new PurchaseHomeShowVo();

			BeanUtils.copyProperties(demandOrders.get(i), purchaseHomeShowVo);
			purchaseHomeShowVo.setDemandCreatetime(GetTime.dateToStr(demandOrders.get(i).getDemandCreatetime()));

			purchaseHomeShowVos.add(purchaseHomeShowVo);
		}
		return purchaseHomeShowVos;
	}
	
	
	/**
	 * 右上角用户信息
	 * @param userId
	 * @return
	 */
	public UserHomeMessageShowVo userHome(int userId){
		UserHomeMessageShowVo userHomeMessageShowVo = new UserHomeMessageShowVo();
		UserInfomation userInfomation = userInfomationMapper.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userInfomation, userHomeMessageShowVo);
		UserScore userScore = userScoreMapper.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userScore, userHomeMessageShowVo);
		return userHomeMessageShowVo;
	}
	
	/**
	 * 返回用户订单详细信息
	 */
	@Override
	public DemandMessageShowVo demandMessage(DemandIdVo demandIdVo) {
		
		DemandOrderViewExample example = new DemandOrderViewExample();
		example.or().andDemandIdEqualTo(demandIdVo.getDemandId());
		List<DemandOrderView> demandOrderViews = demandOrderViewMapper.selectByExample(example);

		DemandMessageShowVo demandMessageShowVo = new DemandMessageShowVo();
		
		BeanUtils.copyProperties(demandOrderViews.get(0), demandMessageShowVo);

		demandMessageShowVo.setExpectedArrivalTime(GetTime.dateToStr(demandOrderViews.get(0)
				.getExpectedArrivalTime()));

		demandMessageShowVo.setDemandCreatetime(GetTime.dateToStr(demandOrderViews.get(0)
				.getDemandCreatetime()));
		
		copyImage(demandIdVo.getDemandId(), demandMessageShowVo);
		copyImageStore(demandIdVo.getDemandId(), demandMessageShowVo);
		
		return demandMessageShowVo;
	}

	@Override
	public OrderIdVo acceptSuccess(DemandIdVo demandIdVo,int userId) {

		OrderIdVo orderIdVo = new OrderIdVo();
		
		int purchaseOrderId = insertPurchaseOrder(userId);// 插入接单表

		int orderId = insertOrderTable(purchaseOrderId,demandIdVo.getDemandId());// 插入订单表

		LatitudeLongitudeExample example = new LatitudeLongitudeExample();
		example.or().andDemandIdEqualTo(demandIdVo.getDemandId());
		LatitudeLongitude latitudeLongitude = new LatitudeLongitude();
		latitudeLongitude.setOrderId(orderId);
		latitudeLongitudeMapper.updateByExampleSelective(latitudeLongitude, example);
		
		DemandOrder record = new DemandOrder();
		record.setDemandId(demandIdVo.getDemandId());
		record.setDemandState(OrderStateConstant.DEMAND_HAS_BEEN_ACCEPT);
		demandOrderMapper.updateByPrimaryKeySelective(record);

		orderIdVo.setOrderId(orderId);
		
		return orderIdVo;
	}
	
	@Override
	public OrderNoCourierShowVo orderMessage(OrderIdVo orderIdVo) {

		OrderNoCourierShowVo orderNoCourierShowVo = new OrderNoCourierShowVo();
		
		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper.selectByExample(example);
		
		BeanUtils.copyProperties(orderNoCourier.get(0), orderNoCourierShowVo);
		orderNoCourierShowVo.setDemandCreatetime(GetTime.dateToStr(orderNoCourier.get(0).getDemandCreatetime()));
		orderNoCourierShowVo.setExpectedArrivalTime(GetTime.dateToStr(orderNoCourier.get(0).getExpectedArrivalTime()));
		orderNoCourierShowVo.setAcceptTime(GetTime.dateToStr(orderNoCourier.get(0).getAcceptTime()));
		
		//复制快递员信息
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<CourierOrder> courierOrder = courierOrderMapper.selectByExample(courierOrderExample);
		if(courierOrder.size() > 0){
			int courierUserId = courierOrder.get(0).getCourierId();
			UserInfomation userInfomation = userInfomationMapper.selectByPrimaryKey(courierUserId);
			orderNoCourierShowVo.setCourierName(userInfomation.getNickname());
			if(courierOrder.get(0).getArriveTime() != null){
				orderNoCourierShowVo.setArriveTime(GetTime.dateToStr(courierOrder.get(0).getArriveTime()));
			}
		}
		
		copyImage(orderNoCourier.get(0).getDemandId(), orderNoCourierShowVo);
		copyImageStore(orderNoCourier.get(0).getDemandId(), orderNoCourierShowVo);
		
		return orderNoCourierShowVo;
	}
	
	

	/*@Override
	public OrderStateShowVo isBuying(OrderIdVo orderIdVo) {

		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		
		changePurchaseState(orderIdVo.getOrderId(),
				OrderStateConstant.PURCHASE_BUYING);//修改代购表状态
		
		orderStateShowVo.setDemandState(OrderStateConstant.DEMAND_PAY);
		orderStateShowVo.setPurchaseState(OrderStateConstant.PURCHASE_BUYING);
		
		return orderStateShowVo;
	}*/

	/*@Override
	public OrderStateShowVo buyComplete(OrderIdVo orderIdVo) {
		
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		
		changePurchaseState(orderIdVo.getOrderId(),
				OrderStateConstant.PURCHASE_BUY_FINISHED);
		
		orderStateShowVo.setDemandState(OrderStateConstant.DEMAND_PAY);
		orderStateShowVo.setPurchaseState(OrderStateConstant.PURCHASE_BUY_FINISHED);

		return orderStateShowVo;

	}*/
	
	@Override
	public OrderStateShowVo determinePrice(
			DeterminePriceSaveVo determinePriceSaveVo) {
		
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(determinePriceSaveVo.getOrderId());
		
		DemandOrderExample example = new DemandOrderExample();
		example.or().andDemandIdEqualTo(orderTable.getDemandId());
		
		DemandOrder demandOrder = new DemandOrder();
		demandOrder.setEstimateThePrice(determinePriceSaveVo.getEstimateThePrice());
		demandOrder.setReward(determinePriceSaveVo.getReward());
		demandOrder.setDemandId(orderTable.getDemandId());
		demandOrderMapper.updateByPrimaryKeySelective(demandOrder);

//		changePurchaseState(determinePriceSaveVo.getOrderId(),OrderStateConstant.PURCHASE_DETERMINE_PRICE);
		
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();

		orderTable.setOrderState(OrderStateConstant.ORDER_CONFRIM);
		OrderTableExample orderTableExample = new OrderTableExample();
		orderTableExample.or().andOrderIdEqualTo(determinePriceSaveVo.getOrderId());
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		
		orderStateShowVo.setOrderState(OrderStateConstant.ORDER_CONFRIM);
		return orderStateShowVo;
	}
	
	
	
	@Override
	public void findCourier(FindCourierSaveVo findCourierSaveVo) {
		
		int orderId = findCourierSaveVo.getOrderId();
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderId);
		int purchaseId = orderTable.getPurchaseId();
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		purchaseOrder.setPurchaseId(purchaseId);
		purchaseOrder.setPurchaseNote(findCourierSaveVo.getPurchaseNote());
		purchaseOrder.setPurchaseAddress(findCourierSaveVo.getPurchaseAddress());
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		
		LatitudeLongitudeExample latitudeLongitudeExample = new LatitudeLongitudeExample();
		latitudeLongitudeExample.or().andOrderIdEqualTo(orderId);
		LatitudeLongitude latitudeLongitude = new LatitudeLongitude();
		latitudeLongitude.setPurchaseLatitude(findCourierSaveVo.getPurchaseLatitude());
		latitudeLongitude.setPurchaseLongitude(findCourierSaveVo.getPurchaseLongitude());
		latitudeLongitudeMapper.updateByExampleSelective(latitudeLongitude, latitudeLongitudeExample);
		
//		orderTable.setOrderState(OrderStateConstant.ORDER_FIND_COURIER);
//		orderTableMapper.updateByPrimaryKeySelective(orderTable);
	}
	
	@Override
	public PayCourierPriceShowVo payCourierPricePage(OrderIdVo orderIdVo) {//支付快递费接口
		PayCourierPriceShowVo payCourierPriceShowVo = new PayCourierPriceShowVo();
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		if(orderNoCourier != null){
			payCourierPriceShowVo.setOrderIdVo(orderIdVo.getOrderId());
			payCourierPriceShowVo.setCourierFees(orderNoCourier.get(0).getCourierFees());
			int vou = 0;
			if(orderNoCourier.get(0).getPurchaseUseVouchaseId() != null){
				UserVouchers userVouchers = userVouchersMapper.selectByPrimaryKey(orderNoCourier.get(0).getPurchaseUseVouchaseId());
				Vouchers vouchers = vouchersMapper.selectByPrimaryKey(userVouchers.getVouchersId());
				payCourierPriceShowVo.setVouchersPrice(vouchers.getVouchersPrice());
				vou = Integer.valueOf(vouchers.getVouchersPrice());
			}
			
			int cou = Integer.valueOf(orderNoCourier.get(0).getCourierFees().substring(0,orderNoCourier.get(0).getCourierFees().length()-1));
			int account = cou - vou;
			payCourierPriceShowVo.setAccount(account+"");
			
			PurchaseOrder purchaseOrder = new PurchaseOrder();
			purchaseOrder.setPurchaseId(orderNoCourier.get(0).getPurchaseId());
			purchaseOrder.setPurchaseFindCourierTime(GetTime.getNowTime());
			purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		}
		
		
		return payCourierPriceShowVo;
	}

	
	@Override
	public OrderIdVo payCourierPricePageVoucher(
			PayCourierPriceSaveVo payCourierPriceSaveVo) {
		OrderIdVo orderIdVo = new OrderIdVo();

		int orderId = payCourierPriceSaveVo.getOrderId();
		int userVouchersId = payCourierPriceSaveVo.getUserVouchersId();

		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(orderId);
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		
		if(orderNoCourier != null){
			PurchaseOrder purchaseOrder = new PurchaseOrder();
			purchaseOrder.setPurchaseId(orderNoCourier.get(0).getPurchaseId());
			purchaseOrder.setPurchaseUseVouchaseId(userVouchersId);
			
			UserVouchers userVouchers = userVouchersMapper.selectByPrimaryKey(userVouchersId);
			Vouchers vouchers = vouchersMapper.selectByPrimaryKey(userVouchers.getVouchersId());
			int vouchasePrice = Integer.valueOf(vouchers.getVouchersPrice());
			int kuaidifei = Integer.valueOf(orderNoCourier.get(0).getCourierFees().substring(0,orderNoCourier.get(0).getCourierFees().length()-1));
			int count = kuaidifei - vouchasePrice;
			
			purchaseOrder.setPurchasePayCourierPrice(count+"");
			purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		}
		
		orderIdVo.setOrderId(orderId);
		return orderIdVo;
	}
	
	@Override
	public void payCourierPrice(OrderIdVo orderIdVo) {
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderIdVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_FIND_COURIER);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
	}
	
	@Override
	public void userPurchaseComments(
			PurchaseCommentsSaveVo purchaseCommentsSaveVo) {

		UserComments userComments = new UserComments();
		BeanUtils.copyProperties(purchaseCommentsSaveVo, userComments);
		
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(purchaseCommentsSaveVo.getOrderId());
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		if(orderNoCouriers.size() > 0){
			userComments.setDemandUserId(orderNoCouriers.get(0).getUserId());
		}
		
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andOrderIdEqualTo(purchaseCommentsSaveVo.getOrderId());
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(courierOrderExample);
		if(courierOrders.size() > 0){
			userComments.setCourierUserId(courierOrders.get(0).getCourierId());
		}
		userCommentsMapper.insertSelective(userComments);
		
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		purchaseOrder.setPurchaseId(orderNoCouriers.get(0).getPurchaseId());
		purchaseOrder.setPurchaseState(OrderStateConstant.PURCHASE_END_COMMENTS);
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		
	}
	
	
	
	/**
	 * 用户接单服务,插入接单表   
	 */
	public int insertPurchaseOrder(int userId) {
		PurchaseOrder record = new PurchaseOrder();
		record.setPurchaseUserId(userId);
		record.setAcceptTime(GetTime.getNowTime());
		record.setPurchaseState(OrderStateConstant.PURCHASE_ACCEPT_SUCCESS);
		purchaseOrderMapper.insertSelective(record);//useGeneratedKeys="true" keyProperty="purchaseId"
		int purchaseOrderId = record.getPurchaseId();

		return purchaseOrderId;
	}

	/**
	 * 用户接单服务,插入订单表
	 */
	public int insertOrderTable(int purchaseId, int demandId) {
		System.out.println("插入订单表");
		OrderTable record = new OrderTable();
		record.setDemandId(demandId);
		record.setPurchaseId(purchaseId);
		record.setOrderState(OrderStateConstant.ORDER_WAIT_CONFRIM);
		orderTableMapper.insertSelective(record);//useGeneratedKeys="true" keyProperty="orderId"
		int orderId = record.getOrderId();
		return orderId;
	}

	/**
	 * 根据地址id，查询地址详细信息
	 * 
	 * @return UserAddressVo
	 */
	public UserAddressVo selectAddressMsg(int addressId) {
		UserAddressVo userAddressVo = new UserAddressVo();

		/*UserAddressExample example = new UserAddressExample();
		example.or().andAddressIdEqualTo(addressId);
		List<UserAddress> userAddress = userAddressMapper.selectByExample(example);*/
		
		UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);

		BeanUtils.copyProperties(userAddress, userAddressVo);

		return userAddressVo;
	}

	/**
	 * 根据订单id，查询订单视图详细信息，不含快递
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderNoCourier selectOrderNoCourier(int orderId) {
		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderIdEqualTo(orderId);
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper
				.selectByExample(example);
		return orderNoCourier.get(0);
	}

	/**
	 * 通过订单id改变purchaseState 的状态
	 * 
	 * @param orderId
	 * @param state
	 *//*
	public int changePurchaseState(int orderId, int state) {
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderId);
		int purchaseId = orderTable.getPurchaseId();
		PurchaseOrder purchaseOrder = purchaseOrderMapper
				.selectByPrimaryKey(purchaseId);
		purchaseOrder.setPurchaseState(state);
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		return 1;
	}*/
	
	/**
	 * 复制商品图片
	 * @param demandId
	 * @param demandMessageShowVo
	 * @return
	 */
	public DemandMessageShowVo copyImage(int demandId,DemandMessageShowVo demandMessageShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderExample imageUrlDemandOrderExample = new ImageUrlDemandOrderExample();
		imageUrlDemandOrderExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrder> images = imageUrlDemandOrderMapper.selectByExample(imageUrlDemandOrderExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getImageUrl());
		}
		
		demandMessageShowVo.setTradeUrlArrayList((ArrayList<String>) iamgeList);
		
		return demandMessageShowVo;
	}
	
	
	/**
	 * 复制商店图片
	 * @param demandId
	 * @param demandMessageShowVo
	 * @return
	 */
	public DemandMessageShowVo copyImageStore(int demandId,DemandMessageShowVo demandMessageShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderStoreExample imageUrlDemandOrderStoreExample = new ImageUrlDemandOrderStoreExample();
		imageUrlDemandOrderStoreExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrderStore> images = imageUrlDemandOrderStoreMapper.selectByExample(imageUrlDemandOrderStoreExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getStoreImageUrl());
		}
		
		demandMessageShowVo.setStoreUrlArrayList((ArrayList<String>) iamgeList);
		
		return demandMessageShowVo;
	}
	
	public OrderNoCourierShowVo copyImage(int demandId,OrderNoCourierShowVo orderNoCourierShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderExample imageUrlDemandOrderExample = new ImageUrlDemandOrderExample();
		imageUrlDemandOrderExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrder> images = imageUrlDemandOrderMapper.selectByExample(imageUrlDemandOrderExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getImageUrl());
		}
		
		orderNoCourierShowVo.setTradeUrlArrayList((ArrayList<String>) iamgeList);
		
		return orderNoCourierShowVo;
	}
	
	public OrderNoCourierShowVo copyImageStore(int demandId,OrderNoCourierShowVo orderNoCourierShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderStoreExample imageUrlDemandOrderStoreExample = new ImageUrlDemandOrderStoreExample();
		imageUrlDemandOrderStoreExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrderStore> images = imageUrlDemandOrderStoreMapper.selectByExample(imageUrlDemandOrderStoreExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getStoreImageUrl());
		}
		
		orderNoCourierShowVo.setStoreUrlArrayList((ArrayList<String>) iamgeList);
		
		return orderNoCourierShowVo;
	}

	@Override
	public OrderStateShowVo acceptRefund(RefundReasonSaveVo refundReasonSaveVo) {
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		
		OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(refundReasonSaveVo.getOrderId());
		
		if(refundReasonSaveVo.getRefundTemp() == 0){
//			OrderTable orderTable = new OrderTable();
//			orderTable.setOrderId(refundReasonSaveVo.getOrderId());
			orderTable.setOrderState(OrderStateConstant.ORDER_PAY);
			orderTableMapper.updateByPrimaryKeySelective(orderTable);
			
			UserRefund userRefund = new UserRefund();
			userRefund.setOrderId(refundReasonSaveVo.getOrderId());
			userRefund.setRefundState(OrderStateConstant.REFUND_NO);
			
			UserRefundExample example = new UserRefundExample();
			example.or().andOrderIdEqualTo(refundReasonSaveVo.getOrderId());
			userRefundMapper.updateByExampleSelective(userRefund, example);
			
			orderStateShowVo.setOrderCourierState(orderTable.getOrderState());
			return orderStateShowVo;
		}else if(refundReasonSaveVo.getRefundTemp() == 1){
//			OrderTable orderTable = new OrderTable();
//			orderTable.setOrderId(refundReasonSaveVo.getOrderId());
			orderTable.setOrderState(OrderStateConstant.ORDER_CANCEL);
			orderTableMapper.updateByPrimaryKeySelective(orderTable);
			
			UserRefund userRefund = new UserRefund();
			userRefund.setOrderId(refundReasonSaveVo.getOrderId());
			userRefund.setRefundState(OrderStateConstant.REFUND_YES);
			
			UserRefundExample example = new UserRefundExample();
			example.or().andOrderIdEqualTo(refundReasonSaveVo.getOrderId());
			userRefundMapper.updateByExampleSelective(userRefund, example);
			
			orderStateShowVo.setOrderCourierState(orderTable.getOrderState());
			return orderStateShowVo;
		}
		return orderStateShowVo;
		
	}

}
